package com.syni.mdd.yhd.common.all.dao.jpa;

import com.syni.mdd.yhd.common.all.dao.BaseJPADao;
import com.syni.mdd.yhd.common.all.entity.coupon.BmsUserReceCoupon;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface BmsUserReceCouponDao extends BaseJPADao<BmsUserReceCoupon,Integer> {
    List<BmsUserReceCoupon> findByUserId(Integer userId);

    List<BmsUserReceCoupon> findByUserIdAndType(Integer userId,Integer type);

    BmsUserReceCoupon findByIdAndStatus(Integer fullCouponId,Integer status);

    List<BmsUserReceCoupon> findByIdIn(Set userReceIds);

    @Query(value = "select count(*) num,burc.business_coupon_id,bbc.limit_once_use from bms_user_rece_coupon burc left join " +
            "bms_business_coupon bbc on burc.business_coupon_id = bbc.id  where burc.user_id = ?1 " +
            "and burc.business_coupon_id in (?2) group by burc.business_coupon_id having num >= bbc.limit_once_use",nativeQuery = true)
    List<Map<String, Object>> countUserReceCouponOver(Integer userId, Set<Integer> businessCouponIds);
}
